<template>
  <div :class="['breadcrumb-box mask-image', !globalStore.breadcrumbIcon && 'no-icon']">
    <el-breadcrumb :separator-icon="ArrowRight">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item v-for="(item, index) in breadcrumbList" :key="index">
          <div class="el-breadcrumb__inner is-link" @click="onBreadcrumbClick(item, index)">
            <template v-if="globalStore.breadcrumbIcon">
              <template v-if="item.attributes?.image">
                <jxtech-svg-icon :icon="item.attributes?.image" :size="16"></jxtech-svg-icon>
              </template>
              <template v-else>
                <jxtech-svg-icon icon="#icon-modular" :size="16"></jxtech-svg-icon>
              </template>
            </template>
            <span class="breadcrumb-title">&nbsp;{{ item?.meta?.title || item.text }}</span>
          </div>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { HOME_URL } from "@/config";
import { useRoute, useRouter } from "vue-router";
import { ArrowRight } from "@element-plus/icons-vue";
import { useAuthStore } from "@/stores/modules/auth";
import { useGlobalStore } from "@/stores/modules/global";

const route = useRoute();
const router = useRouter();
const authStore = useAuthStore();
const globalStore = useGlobalStore();

/**面包屑计算属性 */
const breadcrumbList = computed(() => {
  let breadcrumbData;
  let routeMatched = route.matched[route.matched.length - 1].path.split("/");
  /**判断是编辑或者新增页面 */
  let someFlag = routeMatched.some(item => item == "add" || item == "form");
  if (someFlag) {
    breadcrumbData = authStore.breadcrumbListGet[route.matched[route.matched.length - 1].meta.activeMenu] ?? [];
  } else {
    breadcrumbData = authStore.breadcrumbListGet[route.matched[route.matched.length - 1].path] ?? [];
  }
  // 🙅‍♀️不需要首页面包屑可删除以下判断
  if (breadcrumbData[0].attributes.url !== HOME_URL) {
    const home = {
      attributes: {
        compId: "application",
        image: "icon-home",
        menuCode: "demo:appvue",
        menuOpenMode: null,
        order: null,
        pagePoint: false,
        url: "/home/index",
        vuePath: null
      },
      checked: false,
      children: [],
      id: Math.random(),
      isParent: false,
      level: 2,
      selectType: 0,
      state: "open",
      text: "首页"
    };
    breadcrumbData = [home, ...breadcrumbData];
  }
  return breadcrumbData;
});

// Click Breadcrumb
const onBreadcrumbClick = (item: Menu.MenuOptions, index: number) => {
  console.log(item);
  // if (index !== breadcrumbList.value.length - 1) router.push(item.path);
};
</script>

<style scoped lang="scss">
.breadcrumb-box {
  display: flex;
  align-items: center;
  overflow: hidden;
  .el-breadcrumb {
    white-space: nowrap;
    .el-breadcrumb__item {
      position: relative;
      display: inline-block;
      float: none;
      .el-breadcrumb__inner {
        display: inline-flex;
        align-items: center;
        &.is-link {
          color: var(--el-header-text-color);
          &:hover {
            color: var(--el-color-primary);
          }
        }
        .breadcrumb-icon {
          margin-top: 2px;
          margin-right: 6px;
          font-size: 16px;
        }
        .breadcrumb-title {
        }
      }
      &:last-child .el-breadcrumb__inner,
      &:last-child .el-breadcrumb__inner:hover {
        color: var(--el-header-text-color-regular);
      }
      :deep(.el-breadcrumb__separator) {
        position: relative;
        top: -1px;
      }
    }
  }
}
.no-icon {
  .el-breadcrumb {
    .el-breadcrumb__item {
      top: -2px;
      :deep(.el-breadcrumb__separator) {
        top: 2px;
      }
    }
  }
}
</style>
